package wps.util {
import mx.collections.ArrayCollection;
import mx.controls.Alert;

import wps.constant.IconXMLConst;
import wps.model.Icon;
import wps.model.IconCategory;

public class IconBuilder implements IIconBuilder {

    //--------------------------------------------------------------------------
    //
    // Properties
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    // categories
    //----------------------------------

    [ArrayElementType("wps.model.Icon")]
    private var _icons:ArrayCollection;

    //----------------------------------
    // categories name
    //----------------------------------

    private var _categoriesName:ArrayCollection;

    //--------------------------------------------------------------------------
    //
    // Methods
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    // getCategories
    //----------------------------------

    public function getResultCollection():ArrayCollection {
        return _icons;
    }

    //----------------------------------
    // getCategoryNameCollection
    //----------------------------------

    public function getCategoryNameCollection():ArrayCollection {
        return _categoriesName;
    }

    //----------------------------------
    // builder
    //----------------------------------

    public function build(xmlStr:String):void {
        var xml:XML = new XML(xmlStr);

        // category
        var categoriesXML:XML = xml[IconXMLConst.CATETORY][0];
        if (categoriesXML == null) {
            throw new Error(IconXMLConst.ERR_BUILDER);
        }

        _icons = new ArrayCollection();
        _categoriesName = new ArrayCollection();

        for each (var categoryXML:XML in xml[IconXMLConst.CATETORY]) {
            // category name
            var categoryName:String = categoryXML["@name"];
            _categoriesName.addItem(categoryName);

            // icon
            for each (var iconXML:XML in categoryXML[IconXMLConst.ICON]) {
                var icon:Icon = new Icon();
                icon.url = iconXML.toString();
                icon.category = categoryName;
                _icons.addItem(icon);
            }
        }
    }

}

}
